/*
 * main.c
 *
 * Copyright 2024 dh33ex <dh33ex@riseup.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA or visit <http://www.gnu.org/licenses/>.
 *
 *
 */

#include "parser.h"
#include "generator.h"
#include "gios.h"

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {
    char *in_file = NULL;
    char *out_file = NULL;
    int format = 0; /* bin format by default */

    if (argc == 1) {
        printf(
"uPL0c v0.4 - compiler for uPL0, a micro-programming-language-0 for the RISC-O architecture.\n\n"
"Usage:\n"
"    %s -i <input_file> -o <output_file> [-f <format>]\n\n"
"Formats:\n"
"    bin        a program in binary format\n"
"    txt        a program in text format for a disassembler\n"
"    hdl        a program in verilog syntax for a softcore\n"
        , argv[0]);
        return 0;
    }

    int curr_arg = 1;
    while (curr_arg < argc) {
        if (!strcmp(argv[curr_arg], "-i")) {
            if (curr_arg+1 == argc) { return 1; }

            in_file = argv[curr_arg + 1];
            curr_arg += 2;
        } else if (!strcmp(argv[curr_arg], "-o")) {
            if (curr_arg+1 == argc) { return 1; }

            out_file = argv[curr_arg + 1];
            curr_arg += 2;
        } else if (!strcmp(argv[curr_arg], "-f")) {
            if (curr_arg+1 == argc) { return 1; }

            if (!strcmp(argv[curr_arg+1], "bin")) {
                format = 0;
            } else if (!strcmp(argv[curr_arg+1], "txt")) {
                format = 1;
            } else if (!strcmp(argv[curr_arg+1], "hdl")) {
                format = 2;
            } else {
                printf("Inavlid output format specified.\n");
                return 1;
            }
            curr_arg += 2;
        } else {
            break;
        }
    }

    if (NULL == in_file || NULL == out_file) {
        printf("usage: %s -i <input_file> -o <output_file> [-f <FORMAT: bin/txt/hdl>]\n", argv[0]);
        return 2;
    }

    if (!read_from(in_file)) {
        printf("Cannot open a source file.\n");
        end_write();
        return 3;
    }

    if (compile()) {
        printf("The following code cannot be compiled.\n");
        end_write();
        return 4;
    }

    if (!write_to(out_file)) {
        printf("Cannot create an output file.\n");
        end_write();
        return 5;
    }

    switch (format) {
        case 0:
        write_bin();
        break;

        case 1:
        write_txt();
        break;

        case 2:
        write_code();
        break;

        default:
        printf("This can't be happening, something is terribly wrong!");
        break;
    }

    end_write();

	return 0;
}
